<template>
  <view class="content">
    <view class="app-title ac">
      <view class="fl" @click="back"
        ><image class="back" src="/static/img/back.png"></image></view
      >服务器配置
    </view>

    <view class="detail">
      <u-cell-group class="device-attr">
        <u-field
          v-model="data_list.ip"
          label="服务器地址"
          placeholder="请填写"
        ></u-field>
        <u-field
          v-model="data_list.port"
          label="端口配置"
          placeholder="请填写"
        ></u-field>
      </u-cell-group>
    </view>

    <view class="btn">
      <button class="cancel-btn" @click="back">取消</button
      ><button class="default-btn fr" @click="saveIp">保存</button>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      data_list: {
        ip: "",
        port: "",
      },
    };
  },
  created() {
    this.data_list.ip = uni.getStorageSync("ip");
    this.data_list.port = uni.getStorageSync("port");
  },
  methods: {
    back() {
      uni.navigateBack();
    },
    saveIp() {
      if (this.data_list.ip == "" || this.data_list.port == "")
        return uni.showToast({ title: "请填写IP地址和端口", icon: "none" });
      // this.$store.commit('setBaseUrl',this.data_list);

      // 设置全局地址
      this.$store.state.wsEmerUrl =
        "ws://" + this.data_list.ip + ":" + this.data_list.port;
      this.$store.state.baseEmerUrl =
        "http://" + this.data_list.ip + ":" + this.data_list.port;
      this.$store.state.wsBaseUrl =
        "ws://" + this.data_list.ip + ":" + this.data_list.port;
      this.$store.state.baseUrl =
        "http://" + this.data_list.ip + ":" + this.data_list.port;
	this.$store.state.baseProductUrl = "http://" + this.data_list.ip + ":" + this.data_list.port;
	this.$store.state.baseFaultUrl = "http://" +  this.data_list.ip + ":" + this.data_list.port;
	this.$store.state.baseOrderUrl = "http://" +  this.data_list.ip + ":" + this.data_list.port;
	this.$store.state.wsOrderUrl = "ws://" +  this.data_list.ip + ":" + this.data_list.port;
      uni.setStorageSync("ip", this.data_list.ip);
      uni.setStorageSync("port", this.data_list.port);

      uni.setStorageSync(
        "baseUrl",
        "http://" + this.data_list.ip + ":" + this.data_list.port
      );
      // uni.setStorageSync('baseUrl', 'http://115.29.234.80:8295');

      uni.showToast({ title: "修改成功", icon: "none" });
      setTimeout(() => {
        this.back();
      }, 1500);
    },
  },
};
</script>

<style scoped>
.detail {
  margin-top: 20px;
  height: auto;
}
.btn {
  position: absolute;
  bottom: 3%;
  left: 0;
  width: 100%;
  padding: 0 20px;
}
.btn > button {
  width: 48%;
  display: inline-block;
}
</style>
